package com.ysd.dao.impl;

import java.io.Console;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ysd.entity.Product;
import com.ysd.entity.Productcategory;
import com.ysd.entity.User;
import com.ysd.util.DBManager;

public class UserDaoImpl {
	public int addMessage(User user) {
		String sql = "insert into user (id,username,phone,email,content)values(null,'" + user.getUsername() + "','"
				+ user.getPhone() + "','" + user.getEmail() + "','" + user.getContent() + "') ";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
		// 从数据库中查询
	}

	/**
	 * 修改留言状态为已读
	 * 
	 * @param user
	 * @return
	 */
	public int updateMessage(User user) {
		return DBManager.executeUpdate("update user set status='" + user.getStatus() + "'  where id=" + user.getId());
	}

	/**
	 * 修改留言状态为已回复
	 * 
	 * @param user
	 * @return
	 */

	public int updateMessageReply(User user) {
		return DBManager.executeUpdate(
				"update user set status='已回复',reply='" + user.getReply() + "'  where id=" + user.getId());
	}

	/**
	 * 留言按照状态查询所有(分页查询)
	 * 
	 * @param status
	 * @return
	 */
	public List<User> getUserByStatus(String status, int page, int size) {
		List<User> list = new ArrayList<User>();
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;

		try {
			st = conn.createStatement();
			String sql = "select * from user where 1=1";
			if (status != null && !status.equals("")) {
				sql += " and status='" + status + "'";
			}
			sql += " order by time desc";
			sql += " limit " + (page - 1) * size + "," + size;
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int id = rs.getInt("id");
				String username = rs.getString("username");
				String phone = rs.getString("phone");
				String email = rs.getString("email");
				String content = rs.getString("content");
				String sta = rs.getString("status");
				String time = rs.getString("time");
				User user = new User(id, username, phone, email, content, sta, time);
				list.add(user);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}

	/**
	 * 留言按照状态查询所有(留言数量)
	 * @param status
	 * @return
	 */
	public List<User> getUserByStatusNum(String status) {
		List<User> list = new ArrayList<User>();
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;

		try {
			st = conn.createStatement();
			String sql = "select * from user where 1=1";
			if (status != null && !status.equals("")) {
				sql += " and status='" + status + "'";
			}
			sql += " order by time desc";
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int id = rs.getInt("id");
				String username = rs.getString("username");
				String phone = rs.getString("phone");
				String email = rs.getString("email");
				String content = rs.getString("content");
				String sta = rs.getString("status");
				String time = rs.getString("time");
				User user = new User(id, username, phone, email, content, sta, time);
				list.add(user);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}

	public User getUserById(int id) {
		User user = null;
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			String sql = "SELECT * FROM user where id=" + id;
			;
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String username = rs.getString("username");
				String phone = rs.getString("phone");
				String email = rs.getString("email");
				String content = rs.getString("content");
				String sta = rs.getString("status");
				String time = rs.getString("time");
				user = new User(id, username, phone, email, content, sta, time);
			}
			return user;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	/***
	 *根据id批量删除留言
	 *返回受影响行数 int
	 *
	 *
	 */
	public int delUserByIds(String ids) {
		return DBManager.executeUpdate("delete from user where id in("+ids+")");
	}

}